#_*_ encoding:utf-8 _*_
import copy

### 参数说明
# url: 路由中的name
# valid: 是否显示
# superuser: 是否只有超管显示
MENU_LIST = [
    {'name': u'一级菜单', 'url': 'menu_1_1', 'valid': True, 'children': []},
    {'name': u'一级菜单2', 'valid': True, 'children': [
        {'name': u'二级菜单1', 'url': 'menu_2_1', 'valid': True},
        {'name': u'二级菜单2', 'url': 'menu_2_2', 'valid': True},
        {'name': u'二级菜单3', 'url': 'menu_2_3', 'valid': True}
    ]},
]

def valid_menu(request):
    menu_data = copy.deepcopy(MENU_LIST)
    for i,menu in enumerate(menu_data):
        menu['active'] = False
        # 第一个默认为首页
        if i == 0:
            if request.path == '/':
                menu['active'] = True
                continue
        first_url = menu.get('url','')
        if first_url and first_url in request.path:
            menu['active'] = True
        # 子菜单
        for c in menu['children']:
            # 超管菜单
            if c.get('superuser') and not request.user.is_superuser:
                c['valid'] = False
            c['active'] = False
            if c['url'] in request.path:
                c['active'] = True
                menu['active'] = True
                break
    return menu_data
